/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gui;

import Params.ChatMessage;
import Params.ServerSocketItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;

/**
 *
 * @author Admin
 */
public class ChatFormServer extends javax.swing.JInternalFrame implements ActionListener {

    /**
     * Creates new form ChatFormServer
     */
    public ChatFormServer() {
        initComponents();
    }
    Socket _sockhientai = null;
    public ChatFormServer(String strPort, Socket client) {
        initComponents();
        this.txtInputText.addActionListener(this);
        //initServer(strPort);
        _sockhientai = client;
        int temp = (client.getPort());
        this.title = "SERVER - CLIENT " + temp + "";
    }
    public void onRead(ChatMessage message) {
        this.listChat.add("Client " + message.getName() + " : " + message.getMessage());
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        listChat = new java.awt.List();
        btnSend = new javax.swing.JButton();
        txtInputText = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("CHAT FORM SERVER");

        btnSend.setText("SEND");
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listChat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtInputText, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSend)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listChat, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnSend, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                    .addComponent(txtInputText))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        // TODO add your handling code here:
        if (!this.txtInputText.getText().equals("")) {
            this.listChat.add("Server : " + this.txtInputText.getText());
            for (int i = 0; i < MainSever.listConnection.size(); i++) {
                ServerSocketItem temp = (ServerSocketItem) MainSever.listConnection.elementAt(i);
                Socket _tempsock = temp.getSOCK();
                if (_tempsock.getPort() == _sockhientai.getPort()) {

                    temp.getConnectionChat().bufferedWriter.println(this.txtInputText.getText());
                    temp.getConnectionChat().bufferedWriter.flush();
                    break;
                }
            }
            this.txtInputText.setText("");
        }
    }//GEN-LAST:event_btnSendActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSend;
    private java.awt.List listChat;
    private javax.swing.JTextField txtInputText;
    // End of variables declaration//GEN-END:variables
    @Override
    public void actionPerformed(ActionEvent e) {
//        try {
        // TODO add your handling code here:
       if (!this.txtInputText.getText().equals("")) {
            this.listChat.add("Server : " + this.txtInputText.getText());
            for (int i = 0; i < MainSever.listConnection.size(); i++) {
                ServerSocketItem temp = (ServerSocketItem) MainSever.listConnection.elementAt(i);
                Socket _tempsock = temp.getSOCK();
                if (_tempsock.getPort() == _sockhientai.getPort()) {

                    temp.getConnectionChat().bufferedWriter.println(this.txtInputText.getText());
                    temp.getConnectionChat().bufferedWriter.flush();
                    break;
                }
            }
            this.txtInputText.setText("");
        }
    }
}

